package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.admin.GoodsAddDto;
import com.frejoys.common.dto.admin.GoodsUpDto;
import com.frejoys.common.dto.admin.StoreIdAndNameDto;
import com.frejoys.common.dto.admin.UpShelvesDto;
import com.frejoys.common.dto.app.*;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.IdPageDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.dto.goods.GoodsPageDto;
import com.frejoys.common.dto.goods.GoodsSaveDto;
import com.frejoys.common.dto.store.StoreIdAndPageDto;
import com.frejoys.common.enums.ECategoryStatus;
import com.frejoys.common.enums.EShelves;
import com.frejoys.common.enums.EStatus;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.po.GoodsRepCountPo;
import com.frejoys.common.util.AppConfigUtil;
import com.frejoys.common.util.CommonUtil;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.admin.GoodsListVo;
import com.frejoys.common.vo.app.CategoryListVo;
import com.frejoys.common.vo.app.GoodsSimpleVo;
import com.frejoys.common.vo.app.GoodsVo;
import com.frejoys.common.vo.app.StoreGoodsVo;
import com.frejoys.dao.entity.*;
import com.frejoys.dao.mapper.*;
import com.frejoys.service.CategoryService;
import com.frejoys.service.CommonService;
import com.frejoys.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {


    private final StoreMapper storeMapper;

    private final CategoryService categoryService;

    private final OrderGoodsMapper orderGoodsMapper;

    private final StoreUserMapper storeUserMapper;

    private final StoreActivityMapper storeActivityMapper;

    private final CommonService commonService;

    private final CategoryMapper categoryMapper;

    private final StoreCategoryMapper storeCategoryMapper;

    @Override
    public Goods getInfo(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public IPage<GoodsListVo> listInfo(StoreIdAndNameDto dto) {
        Page<Goods> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        
        if (dto.getStoreId() != null){
            queryWrapper.eq(Goods::getStoreId, dto.getStoreId());
        }
        if (StrUtil.isNotEmpty(dto.getName())){
            List<Integer> ids = baseMapper.getGoodsNameById(dto.getName());
            if (ids.isEmpty()){
                return new Page<>();
            }
            queryWrapper.in(Goods::getId,ids);
        }
        Page<Goods> goodsPage = baseMapper.selectPage(page, queryWrapper);
        IPage<GoodsListVo> goodsListPage = goodsPage.convert(goods -> BeanUtil.copyProperties(goods, GoodsListVo.class));
        List<GoodsListVo> list = goodsListPage.getRecords();
        
        if (!list.isEmpty()){
            List<Integer> storeIds = list.stream().map(GoodsListVo::getStoreId).toList();
            List<Integer> categoryIds = list.stream().map(GoodsListVo::getCategoryId).toList();
            List<Integer> storeCategoryIds = list.stream().map(GoodsListVo::getStoreCategoryId).toList();

            //查询店铺信息
            LambdaQueryWrapper<Store> storeWrapper = new LambdaQueryWrapper<>();
            storeWrapper.select(Store::getId,Store::getName).in(Store::getId,storeIds);
            List<Store> storeList = storeMapper.selectList(storeWrapper);
            Map<Integer, String> storeMap = storeList.stream().collect(Collectors.toMap(Store::getId, Store::getName));

            //查询品类信息
            LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
            categoryWrapper.select(Category::getId,Category::getName).in(Category::getId,categoryIds);
            List<Category> categoryList = categoryMapper.selectList(categoryWrapper);
            Map<Integer, String> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, Category::getName));

            //查询店铺分类信息
            LambdaQueryWrapper<StoreCategory> storeCategoryWrapper = new LambdaQueryWrapper<>();
            storeCategoryWrapper.select(StoreCategory::getId,StoreCategory::getName).in(StoreCategory::getId,storeCategoryIds);
            List<StoreCategory> storeCategoryList = storeCategoryMapper.selectList(storeCategoryWrapper);
            Map<Integer, String> storeCategoryMap = storeCategoryList.stream().collect(Collectors.toMap(StoreCategory::getId, StoreCategory::getName));

            list.forEach(vo ->{
                vo.setStoreName(storeMap.get(vo.getStoreId()));
                vo.setCategoryName(categoryMap.get(vo.getCategoryId()));
                vo.setStoreCategoryName(storeCategoryMap.get(vo.getStoreCategoryId()));
                vo.setCoverUrl(AppConfigUtil.getUrlPrefix(vo.getCover()));
            });
        }

        return goodsListPage;
    }

//    @Override
//    public boolean goodsOnSale(IdDto dto) {
//        return update(Wrappers.<Goods>lambdaUpdate().set(Goods::getShelves, EShelves.onSale.getValue()).eq(Goods::getId, dto.getId()));
//    }


    //首页商品列表
//    @Override
//    public List<GoodsVo> listHomeInfo(PageDto pageDto) {
//        Page<Store> page = new Page<>(pageDto.getPage(), pageDto.getSize());
//        page.setSearchCount(false);
//        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Store::getShelves, EShelves.onSale.getValue());
//        queryWrapper.eq(Store::getStatus, EStatus.on.getValue());
////        List<Store> storeList = storeMapper.selectList(queryWrapper);
//        List<Store> storeList = storeMapper.selectPage(page, queryWrapper).getRecords();
//
//        Map<Integer, String> storeIdNameMap = storeList.stream().collect(Collectors.toMap(Store::getId, Store::getName));
//        Set<Integer> storeIds = storeIdNameMap.keySet();
//        List<GoodsVo> result = baseMapper.selectStoreTopGoods(storeIds);
//
//        for (GoodsVo goodsVo : result) {
//            goodsVo.setStoreName(storeIdNameMap.get(goodsVo.getStoreId()));
//        }
//        return result;
//    }


    //筛选商品列表 排序
    @Override
    public List<GoodsVo> goodsFiltrationList(GoodsFiltrationListDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        List<Integer> categoryIds = new ArrayList<>();

        if (dto.getCategoryId() != null && dto.getSite() !=null){
            //查出细类
            CategoryPidDto categoryPidDto = new CategoryPidDto();
            categoryPidDto.setId(dto.getCategoryId());
            categoryPidDto.setSite(dto.getSite());
            List<CategoryListVo> categoryListVos = categoryService.categoryListInfo(categoryPidDto, categoryService.listInfo());

            categoryIds = categoryListVos.stream().map(CategoryListVo::getId).toList();

        }
//        //没查出品类
//        if (categoryIds.isEmpty() && i == 1){
//            return list;
//        }

        //排序
        List<GoodsVo> list = baseMapper.goodsList(page, categoryIds, dto.getSort(),EStatus.on.getValue(),EShelves.onSale.getValue());

            //把店铺名插入进去
            if (!list.isEmpty()) {
                List<Integer> storeIds = list.stream().map(GoodsVo::getStoreId).toList();

                //店铺名字
                List<Store> storeList = storeMapper.getStoreList(storeIds);
                Map<Integer, Store> storeMap = storeList.stream().collect(Collectors.toMap(Store::getId, store -> store));

                Map<Integer, BigDecimal> totalMoney = new HashMap<>();

                //总累计金额
                List<StoreActivity> storeActivityList = storeActivityMapper.totalMoneyAll(storeIds, TimeUtil.unixTime(), EShelves.onSale.getValue(),EStatus.on.getValue());

                if (!storeActivityList.isEmpty()){
                    totalMoney = storeActivityList.stream().collect(Collectors.toMap(StoreActivity::getStoreId,StoreActivity::getTotalMoney));

                }

                for (GoodsVo goodsVo : list) {

                    goodsVo.setSoldCount(goodsVo.getSoldCount() + goodsVo.getVarSoldCount());

                    if (storeMap.containsKey(goodsVo.getStoreId())){
                        Store store = storeMap.get(goodsVo.getStoreId());
                        goodsVo.setStoreName(store.getName());

                        if (!Double.isNaN(dto.getLng()) && !Double.isNaN(dto.getLat()) && dto.getLng() != 0 && dto.getLat() != 0){
                            double distance = commonService.calculateDistance(dto.getLat(), dto.getLng(), store.getLat(), store.getLng());
                            goodsVo.setDistance(distance);
                        }
                    }
                    if (StrUtil.isNotEmpty(goodsVo.getCover())) {
                        goodsVo.setCover(CommonUtil.getFirstCover(goodsVo.getCover()));

                    }
                    if (totalMoney.containsKey(goodsVo.getStoreId())){
                        goodsVo.setTotalMoney(totalMoney.get(goodsVo.getStoreId()));
                    }
                }

            }
            return list;
    }


    //商品详情
    @Override
    public GoodsVo GoodsDetails(Integer id) {

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Goods::getId, id)
                .eq(Goods::getShelves,EShelves.onSale.getValue())
                .eq(Goods::getStatus,EStatus.on.getValue());

        Goods goods = getOnly(wrapper);
        if (goods == null) {
            return null;
        }

        Store store = storeMapper.getNameInfo(goods.getStoreId());

        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setCover(AppConfigUtil.getUrlPrefix(goods.getCover()));
        goodsVo.setStoreCategoryId(goods.getStoreCategoryId());
        goodsVo.setName(goods.getName());
        goodsVo.setId(goods.getId());
        goodsVo.setPrice(goods.getPrice());
        goodsVo.setOriPrice(goods.getOriPrice());
        goodsVo.setSoldCount(goods.getSoldCount() + goodsVo.getVarSoldCount());
        goodsVo.setStoreId(goods.getStoreId());
        goodsVo.setStoreName(store != null ? store.getName() : "");
        return goodsVo;
    }

    @Override
    public boolean addInfo(GoodsAddDto dto) {
        Goods goods = BeanUtil.copyProperties(dto, Goods.class);
        goods.setShelves(EShelves.offShelf.getValue());
        goods.setStatus(EStatus.on.getValue());
        return save(goods);
    }

    @Override
    public boolean upInfo(GoodsUpDto dto) {

        boolean goodsOnSale = baseMapper.exists(Wrappers.<Goods>lambdaQuery().eq(Goods::getId, dto.getId())
                .and(qw -> {
                    qw.eq(Goods::getShelves, EShelves.onSale.getValue());
                    qw.or();
                    qw.eq(Goods::getShelves, EShelves.applyfor.getValue());
                }));
        if (goodsOnSale) {
            throw new GeneralException(HttpStatus.onSaleEditError);
        }

        LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Goods::getId, dto.getId());
        if (StrUtil.isNotEmpty(dto.getName())) {
            wrapper.set(Goods::getName, dto.getName());
        }
        if (StrUtil.isNotEmpty(dto.getCover())) {
            wrapper.set(Goods::getCover, dto.getCover());
        }
        if (dto.getPrice() != null) {
            wrapper.set(Goods::getPrice, dto.getPrice());
        }
        if (dto.getOriPrice() != null) {
            wrapper.set(Goods::getOriPrice, dto.getOriPrice());
        }
        if (dto.getCategoryId() != null) {
            wrapper.set(Goods::getCategoryId, dto.getCategoryId());
        }
        if (dto.getStoreCategoryId() != null) {
            wrapper.set(Goods::getStoreCategoryId, dto.getStoreCategoryId());
        }
        if (dto.getVarSoldCount() != null) {
            wrapper.set(Goods::getVarSoldCount, dto.getVarSoldCount());
        }
        if (dto.getSort() != null) {
            wrapper.set(Goods::getSort, dto.getSort());
        }
        return update(wrapper);
    }

    @Override
    public boolean delInfo(Integer id) {
        LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Goods::getId, id);
        wrapper.set(Goods::getStatus, ECategoryStatus.invalid.getValue());
        return update(wrapper);
    }


    //根据品类细类筛选商品列表 排序
    @Override
    public List<GoodsVo> sonGoodsFiltrationList(GoodsFiltrationListDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        //排序
        List<GoodsVo> list = baseMapper.goodsList(page, Collections.singletonList(dto.getCategoryId()), dto.getSort(),EStatus.on.getValue(),EShelves.onSale.getValue());

        if (!list.isEmpty()) {
            List<Integer> storeIds = list.stream().map(GoodsVo::getStoreId).toList();

            //店铺名
            List<Store> storeList = storeMapper.getStoreList(storeIds);
            Map<Integer, String> nameMap = storeList.stream().collect(Collectors.toMap(Store::getId, Store::getName));

            Map<Integer, BigDecimal> totalMoney = new HashMap<>();

            //总累计金额
            List<StoreActivity> storeActivityList = storeActivityMapper.totalMoneyAll(storeIds,TimeUtil.unixTime(),EShelves.onSale.getValue(),EStatus.on.getValue());

            if (!storeActivityList.isEmpty()) {
                totalMoney = storeActivityList.stream().collect(Collectors.toMap(StoreActivity::getStoreId,StoreActivity::getTotalMoney));
            }

            for (GoodsVo goodsVo : list) {

                if (StrUtil.isNotEmpty(goodsVo.getCover())){
                    goodsVo.setCover(CommonUtil.getFirstCover(goodsVo.getCover()));
                }
                goodsVo.setStoreName(nameMap.get(goodsVo.getStoreId()));

                if (totalMoney.containsKey(goodsVo.getStoreId())){
                    goodsVo.setTotalMoney(totalMoney.get(goodsVo.getStoreId()));
                }
            }

        }
        return list;
    }

    /**
     * 用户查询店铺商品
     *
     * @param dto
     * @return
     */
    @Override
    public List<GoodsVo> userGoodsList(StoreIdDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getStoreId, dto.getId())
                .eq(Goods::getShelves,EShelves.onSale.getValue())
                .eq(Goods::getStatus,EStatus.on.getValue())
                .orderByAsc(Goods::getSort);
        List<Goods> list = baseMapper.selectPage(page, wrapper).getRecords();

        List<GoodsVo> goodsList = new ArrayList<>();

        if (!list.isEmpty()) {
            //搜索店铺信息 插入店铺名
            Store store = storeMapper.getNameInfo(dto.getId());

            list.forEach(goods -> {
                GoodsVo goodsVo = new GoodsVo();

                goodsVo.setCover(CommonUtil.getFirstCover(goods.getCover()));
                goodsVo.setName(goods.getName());
                goodsVo.setId(goods.getId());
                goodsVo.setPrice(goods.getPrice());
                goodsVo.setOriPrice(goods.getOriPrice());
                goodsVo.setSoldCount(goods.getSoldCount());
                goodsVo.setStoreId(goods.getStoreId());
                goodsVo.setStoreName(store != null ? store.getName() : "");
                goodsList.add(goodsVo);
            });
        }
        return goodsList;
    }


    //店铺下的商品
    @Override
    public List<StoreGoodsVo> storeGoodsList(StoreIdAndPageDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getStoreId, dto.getId());
        List<Goods> list = baseMapper.selectPage(page, wrapper).getRecords();

        List<StoreGoodsVo> goodsList = new ArrayList<>();

        if (!list.isEmpty()) {
            //搜索店铺信息 插入店铺名
            Store store = storeMapper.getNameInfo(dto.getId());

            list.forEach(goods -> {
                StoreGoodsVo goodsVo = new StoreGoodsVo();
                BeanUtil.copyProperties(goods,goodsVo);
                goodsVo.setFname(goods.getCover());
                goodsVo.setCover(CommonUtil.getFirstCover(goods.getCover()));
                goodsVo.setStoreName(store.getName());
                goodsList.add(goodsVo);
            });
        }
        return goodsList;
    }

    @Override
    public List<StoreGoodsVo> goodsList(PageDto dto, Long userId) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        List<Integer> storeIds = storeUserMapper.searchStoreIds(userId);

        if (CollectionUtil.isEmpty(storeIds)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Goods::getStoreId, storeIds).ne(Goods::getStatus, EStatus.del.getValue());
        List<Goods> list = baseMapper.selectList(page,wrapper);

        List<StoreGoodsVo> goodsList = new ArrayList<>();

        if (!list.isEmpty()) {

            LambdaQueryWrapper<Store> storeWrapper = new LambdaQueryWrapper<>();
            storeWrapper.select(Store::getId, Store::getName).in(Store::getId, storeIds);
            //搜索店铺信息 插入店铺名
            List<Store> stores = storeMapper.selectList(storeWrapper);
            Map<Integer, String> storeMap = stores.stream().collect(Collectors.toMap(Store::getId, Store::getName));

            list.forEach(goods -> {
                StoreGoodsVo goodsVo = new StoreGoodsVo();
                BeanUtil.copyProperties(goods,goodsVo);
                goodsVo.setFname(goods.getCover());
                goodsVo.setCover(AppConfigUtil.getUrlPrefix(goods.getCover()));
                goodsVo.setStoreName(storeMap.get(goodsVo.getStoreId()));
                goodsList.add(goodsVo);
            });
        }
        return goodsList;
    }

    @Override
    public List<Goods> getGoodsPriceRepCountInfo(Integer storeId, List<Integer> goodsId) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Goods::getId, Goods::getPrice, Goods::getRepCount).eq(Goods::getStoreId, storeId).in(Goods::getId, goodsId).eq(Goods::getStatus, EStatus.on.getValue()).eq(Goods::getShelves, EShelves.onSale.getValue());
        return list(wrapper);
    }

    @Override
    public Integer disposeGoodsRepCount(List<GoodsRepCountPo> countPos) {
        return baseMapper.disposeGoodsRepCount(countPos, countPos.stream().map(GoodsRepCountPo::getId).toList());
    }

    @Override
    public List<Goods> getListByStoreId(IdPageDto dto) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Goods::getStoreId, dto.getId());
        lambdaQueryWrapper.orderByAsc(Goods::getSort);
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);
        return baseMapper.selectPage(page, lambdaQueryWrapper).getRecords();
    }


    @Override
    public Page<Goods> pageGoods(GoodsPageDto pageDto) {
        return baseMapper.selectPage(new Page<>(pageDto.getPage(), pageDto.getSize()), Wrappers.lambdaQuery());
    }

    @Override
    public boolean saveGoods(GoodsSaveDto goodsSaveDto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsSaveDto, goods);
        //验证该商品是否存在
        if (validate(goodsSaveDto.getId(), goodsSaveDto)) {
            throw new GeneralException(HttpStatus.goodsIsExist);
        }
        saveOrUpdate(goods);
        return true;
    }


    @Override
    public boolean goodsUp(IdDto dto) {
        return update(Wrappers.<Goods>lambdaUpdate().set(Goods::getShelves, EShelves.applyfor.getValue()).eq(Goods::getId, dto.getId()));
    }

    @Override
    public boolean goodsDown(IdDto dto) {
        return update(Wrappers.<Goods>lambdaUpdate().set(Goods::getShelves, EShelves.offShelf.getValue()).eq(Goods::getId, dto.getId()));
    }

    @Override
    public List<GoodsVo> selectGoodsList(GoodsNameDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        List<GoodsVo> list = baseMapper.selectGoodsList(page,dto.getName(),EStatus.on.getValue(),EShelves.onSale.getValue());

        if (!list.isEmpty()) {
            list.forEach(goodsVo -> {

                if (StrUtil.isNotEmpty(goodsVo.getCover())){
                    goodsVo.setCover(CommonUtil.getFirstCover(goodsVo.getCover()));
                }

                Store store = storeMapper.getNameInfo(goodsVo.getStoreId());
                goodsVo.setStoreName(store.getName());

                List<StoreActivity> storeActivityList = storeActivityMapper.totalMoneyAll(Collections.singletonList(goodsVo.getStoreId()),TimeUtil.unixTime(),EShelves.onSale.getValue(),EStatus.on.getValue());

                if (!storeActivityList.isEmpty()) {
                    goodsVo.setTotalMoney(storeActivityList.get(0).getTotalMoney());
                }
            });
        }
        return list;
    }

    private boolean validate(Integer goodsId, GoodsSaveDto dto) {
        Goods goods = getOnly(Wrappers.<Goods>lambdaQuery().eq(Goods::getCategoryId, dto.getCategoryId())
                .eq(Goods::getName, dto.getName())
                .eq(Goods::getStoreId, dto.getStoreId())
                .ne(ObjectUtil.isNotEmpty(goodsId), Goods::getId, goodsId));
        return ObjectUtil.isNotEmpty(goods);
    }


    @Override
    public List<GoodsSimpleVo> storeGoodsAndCategoryList(StoreIdDto dto) {
        Page page = new Page<>(dto.getPage(), dto.getSize());
        page.setSearchCount(false);

        //查出店铺下商品信息
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Goods::getStoreId,dto.getId())
                .eq(Goods::getShelves,EShelves.onSale.getValue())
                .eq(Goods::getStatus,EStatus.on.getValue());

        if (dto.getStoreCategoryId() != null){
            wrapper.eq(Goods::getStoreCategoryId,dto.getStoreCategoryId());
        }
        wrapper.orderByAsc(Goods::getSort);

        List<Goods> goodsList = baseMapper.selectPage(page, wrapper).getRecords();
        List<GoodsSimpleVo> list = new ArrayList<>();

        if (!goodsList.isEmpty()){
            goodsList.forEach(goods -> {
                GoodsSimpleVo vo = new GoodsSimpleVo();

                if (StrUtil.isNotEmpty(goods.getCover())){
                    vo.setCover(CommonUtil.getFirstCover(goods.getCover()));
                }
                vo.setId(goods.getId());
                vo.setPrice(goods.getPrice());
                vo.setSoldCount(goods.getSoldCount() + goods.getVarSoldCount());
                vo.setName(goods.getName());
                vo.setOriPrice(goods.getOriPrice());
                list.add(vo);
            });
        }
        return list;
    }

    @Override
    public List<Integer> getGoodsIdList(Integer page, Integer size) {
        Page p = new Page<>(page, size);
        p.setSearchCount(false);

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Goods::getId).eq(Goods::getStatus, EStatus.on.getValue()).eq(Goods::getShelves, EShelves.onSale.getValue());
        List<Goods> list = baseMapper.selectPage(p, wrapper).getRecords();
        return list.isEmpty() ? new ArrayList<>() : list.stream().map(Goods::getId).toList();
    }



    @Override
    public boolean upGoodsShelves(UpShelvesDto dto) {

        if (dto.getShelves() == EShelves.offShelf.getValue() || dto.getShelves() == EShelves.onSale.getValue()){
            LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(Goods::getId,Goods::getStoreId)
                    .eq(Goods::getId,dto.getId());
            Goods goods = getOnly(wrapper);

            if (ObjectUtil.isEmpty(goods)){
                throw new GeneralException(HttpStatus.goodsNotFound);
            }

            if (dto.getShelves() == EShelves.offShelf.getValue()){
                //校验店铺商品数量
                int count = baseMapper.validGoodsCount(goods.getStoreId(), EShelves.onSale.getValue(), EStatus.on.getValue());
                Store store = storeMapper.getShelvesInfo(goods.getStoreId());

                if (count <= 1 && ObjectUtil.isNotEmpty(store) && store.getShelves() != EShelves.offShelf.getValue()){
                    throw new GeneralException(HttpStatus.soldOutOrderError);
                }
            }

            baseMapper.upShelvesInfo(dto.getId(), dto.getShelves());
        }
        return true;
    }


}
